#########################
#   Author : Leon yu    #
#   Date : 2025/06/03   #
#   Id : SM2772         #
#########################
from util import Utils
from bussinessApis import *


class ApiReqs(object):
    apiInitData = Utils.loadData(apiInitDataPath, True)
    apisUri = apiInitData['apisUri']
    apisInfo = apiInitData['apisInfo']
    refreshToken = apiInitData['refreshToken']
    accessToken = apiInitData['accessToken']
    
    def __init__(self, env, orgId):
        self.env = env
        self.orgId = orgId
        self.url = f"https://{self.apisUri}/v3/eco/model/{orgId}/retail/$serviceName/{env}"
        self.bussinessRequestHost = Utils.bussinessRequestHost()
        self.commonHeaders = Utils.commonHeaders
        self.appId = Utils.appId
        self.httpDataHeaders = {
                        "x-org-id": self.orgId,
                        "max-app-id": self.appId,
                        "sunmi-appid": self.appId,
                        "x-client-name": "",
                        "x-token": self.refreshToken,
                        "max-token": self.accessToken,
                        "sunmi-token": self.accessToken,
                        "content-type": "application/json"
                    }
        self.bussinessRequestClass = Utils.bussinessRequestClass
        self.template = Utils.template
        
    # 营业概览
    def queryBusinessTrendReportReq(self, storeId, businessType, startTime:int, endTime:int):
        apiInfo = self.apisInfo['queryBusinessTrendReport']
        reqMethod = apiInfo['method']
        serviceName = apiInfo['serviceName']
        reqBody = self.bussinessRequestClass(apiInfo=apiInfo, header={}, body={})
        apiUrl = self.template(self.url, {"serviceName": serviceName})
        reqBody['httpData']['uri'] = apiUrl
        reqBody['httpData']['header'] = self.httpDataHeaders
        reqBody['httpData']['body'] = {
            "startTime": startTime,
			"endTime": endTime,
			"timeType": "DAY",
			"businessType": businessType, # 营业收入:TURNOVER, 订单数:ORDERNUMS, 商品数量:ITEMNUMS, 毛利润:GROSS
			"platform": "",
			"storeId": storeId
        }
        reqInfo = dict()
        reqInfo['reqUrl'] = f"https://{self.bussinessRequestHost}"
        reqInfo['reqHeaders'] = self.commonHeaders
        reqInfo['reqBody'] = reqBody
        reqInfo['reqMethod'] = reqMethod
        return reqInfo
    
    # 订单管理-全部订单
    def queryOrderListReq(self, storeId, startTime:int, endTime:int, status:str="", pageNum=1, pageSize=500, subPlatform=[]):
        apiInfo = self.apisInfo['queryOrderList']
        reqMethod = apiInfo['method']
        serviceName = apiInfo['serviceName']
        reqBody = self.bussinessRequestClass(apiInfo=apiInfo, header={}, body={})
        apiUrl = self.template(self.url, {"serviceName": serviceName})
        reqBody['httpData']['uri'] = apiUrl
        reqBody['httpData']['header'] = self.httpDataHeaders
        reqBody['httpData']['body'] = {
            "startTime": startTime,
			"endTime": endTime,
			"pageNum": pageNum,
			"pageSize": pageSize, 
			"status": status,
			"storeId": storeId,
            "subPlatform" : subPlatform
        }
        reqInfo = dict()
        reqInfo['reqUrl'] = f"https://{self.bussinessRequestHost}"
        reqInfo['reqHeaders'] = self.commonHeaders
        reqInfo['reqBody'] = reqBody
        reqInfo['reqMethod'] = reqMethod
        return reqInfo
    
    # 销售统计
    def queryStoreSalesReportReq(self, storeId, startTime:int, endTime:int, pageNum:int=1, pageSize:int=1000, platform=None):
        apiInfo = self.apisInfo['queryStoreSalesReport']
        reqMethod = apiInfo['method']
        serviceName = apiInfo['serviceName']
        reqBody = self.bussinessRequestClass(apiInfo=apiInfo, header={}, body={})
        apiUrl = self.template(self.url, {"serviceName": serviceName})
        reqBody['httpData']['uri'] = apiUrl
        reqBody['httpData']['header'] = self.httpDataHeaders
        reqBody['httpData']['body'] = {
            "startTime": startTime,
			"endTime": endTime,
			"pageNum": pageNum,
			"pageSize": pageSize, 
			"platform": platform,
			"storeId": storeId
        }
        reqInfo = dict()
        reqInfo['reqUrl'] = f"https://{self.bussinessRequestHost}"
        reqInfo['reqHeaders'] = self.commonHeaders
        reqInfo['reqBody'] = reqBody
        reqInfo['reqMethod'] = reqMethod
        return reqInfo
    
    # 销售明细
    def querySalesItemReportReq(self, storeId, startTime:int, endTime:int, pageNum:int=1, pageSize:int=1000, categoryName=""):
        apiInfo = self.apisInfo['querySalesItemReport']
        reqMethod = apiInfo['method']
        serviceName = apiInfo['serviceName']
        reqBody = self.bussinessRequestClass(apiInfo=apiInfo, header={}, body={})
        apiUrl = self.template(self.url, {"serviceName": serviceName})
        reqBody['httpData']['uri'] = apiUrl
        reqBody['httpData']['header'] = self.httpDataHeaders
        reqBody['httpData']['body'] = {
            "startTime": startTime,
			"endTime": endTime,
			"pageNum": pageNum,
			"pageSize": pageSize, 
			"categoryName": categoryName,
			"storeId": storeId
        }
        reqInfo = dict()
        reqInfo['reqUrl'] = f"https://{self.bussinessRequestHost}"
        reqInfo['reqHeaders'] = self.commonHeaders
        reqInfo['reqBody'] = reqBody
        reqInfo['reqMethod'] = reqMethod
        return reqInfo

    # 折扣明细
    def queryDiscountDetailReportReq(self, storeId, startTime:int, endTime:int, pageNum:int=1, pageSize:int=1000, userId=""):
        apiInfo = self.apisInfo['queryDiscountDetailReport']
        reqMethod = apiInfo['method']
        serviceName = apiInfo['serviceName']
        reqBody = self.bussinessRequestClass(apiInfo=apiInfo, header={}, body={})
        apiUrl = self.template(self.url, {"serviceName": serviceName})
        reqBody['httpData']['uri'] = apiUrl
        reqBody['httpData']['header'] = self.httpDataHeaders
        reqBody['httpData']['body'] = {
            "startTime": startTime,
			"endTime": endTime,
			"pageNum": pageNum,
			"pageSize": pageSize, 
			"userId": userId,
			"storeId": storeId
        }
        reqInfo = dict()
        reqInfo['reqUrl'] = f"https://{self.bussinessRequestHost}"
        reqInfo['reqHeaders'] = self.commonHeaders
        reqInfo['reqBody'] = reqBody
        reqInfo['reqMethod'] = reqMethod
        return reqInfo
    
    # 税目明细
    def queryTaxIncomeReportReq(self, storeId, startTime:int, endTime:int, pageNum:int=1, pageSize:int=1000):
        apiInfo = self.apisInfo['queryTaxIncomeReport']
        reqMethod = apiInfo['method']
        serviceName = apiInfo['serviceName']
        reqBody = self.bussinessRequestClass(apiInfo=apiInfo, header={}, body={})
        apiUrl = self.template(self.url, {"serviceName": serviceName})
        reqBody['httpData']['uri'] = apiUrl
        reqBody['httpData']['header'] = self.httpDataHeaders
        reqBody['httpData']['body'] = {
            "startTime": startTime,
			"endTime": endTime,
			"pageNum": pageNum,
			"pageSize": pageSize, 
			"storeId": storeId
        }
        reqInfo = dict()
        reqInfo['reqUrl'] = f"https://{self.bussinessRequestHost}"
        reqInfo['reqHeaders'] = self.commonHeaders
        reqInfo['reqBody'] = reqBody
        reqInfo['reqMethod'] = reqMethod
        return reqInfo
        
    # 税目明细-税目名称 queryTaxDetailReport
    def queryTaxDetailReportReq(self, storeId, startTime:int, endTime:int, pageNum:int=1, pageSize:int=1000):
        apiInfo = self.apisInfo['queryTaxDetailReport']
        reqMethod = apiInfo['method']
        serviceName = apiInfo['serviceName']
        reqBody = self.bussinessRequestClass(apiInfo=apiInfo, header={}, body={})
        apiUrl = self.template(self.url, {"serviceName": serviceName})
        reqBody['httpData']['uri'] = apiUrl
        reqBody['httpData']['header'] = self.httpDataHeaders
        reqBody['httpData']['body'] = {
            "startTime": startTime,
			"endTime": endTime,
			"pageNum": pageNum,
			"pageSize": pageSize, 
			"storeId": storeId
        }
        reqInfo = dict()
        reqInfo['reqUrl'] = f"https://{self.bussinessRequestHost}"
        reqInfo['reqHeaders'] = self.commonHeaders
        reqInfo['reqBody'] = reqBody
        reqInfo['reqMethod'] = reqMethod
        return reqInfo

    # 储值报表
    def eMemberBalanceReportReq(self, storeId, startTime:int, endTime:int, pageNum:int=1, pageSize:int=1000, searchText="", staffId=""):
        apiInfo = self.apisInfo['eMemberBalanceReport']
        reqMethod = apiInfo['method']
        serviceName = apiInfo['serviceName']
        reqBody = self.bussinessRequestClass(apiInfo=apiInfo, header={}, body={})
        apiUrl = self.template(self.url, {"serviceName": serviceName})
        reqBody['httpData']['uri'] = apiUrl
        reqBody['httpData']['header'] = self.httpDataHeaders
        reqBody['httpData']['body'] = {
            "startTime": startTime,
			"endTime": endTime,
			"pageNum": pageNum,
			"pageSize": pageSize, 
			"storeId": storeId,
            "searchText" : searchText,
            "staffId" : staffId
        }
        reqInfo = dict()
        reqInfo['reqUrl'] = f"https://{self.bussinessRequestHost}"
        reqInfo['reqHeaders'] = self.commonHeaders
        reqInfo['reqBody'] = reqBody
        reqInfo['reqMethod'] = reqMethod
        return reqInfo
    
    # 员工绩效 queryEmployPerformanceReport
    def queryEmployPerformanceReportReq(self, storeId, startTime:int, endTime:int, pageNum:int=1, pageSize:int=1000, employeeId=""):
        apiInfo = self.apisInfo['queryEmployPerformanceReport']
        reqMethod = apiInfo['method']
        serviceName = apiInfo['serviceName']
        reqBody = self.bussinessRequestClass(apiInfo=apiInfo, header={}, body={})
        apiUrl = self.template(self.url, {"serviceName": serviceName})
        reqBody['httpData']['uri'] = apiUrl
        reqBody['httpData']['header'] = self.httpDataHeaders
        reqBody['httpData']['body'] = {
            "startTime": startTime,
			"endTime": endTime,
			"pageNum": pageNum,
			"pageSize": pageSize, 
			"storeId": storeId,
            "employeeId" : employeeId
        }
        reqInfo = dict()
        reqInfo['reqUrl'] = f"https://{self.bussinessRequestHost}"
        reqInfo['reqHeaders'] = self.commonHeaders
        reqInfo['reqBody'] = reqBody
        reqInfo['reqMethod'] = reqMethod
        return reqInfo
    
    # 新营业概览-营业
    def eBusinessReportReq(self, storeId, startTime:int, endTime:int, platform=None):
        apiInfo = self.apisInfo['eBusinessReport']
        reqMethod = apiInfo['method']
        serviceName = apiInfo['serviceName']
        reqBody = self.bussinessRequestClass(apiInfo=apiInfo, header={}, body={})
        apiUrl = self.template(self.url, {"serviceName": serviceName})
        reqBody['httpData']['uri'] = apiUrl
        reqBody['httpData']['header'] = self.httpDataHeaders
        reqBody['httpData']['body'] = {
            "startTime": startTime,
			"endTime": endTime,
			"platform": platform,
			"storeId": storeId
        }
        reqInfo = dict()
        reqInfo['reqUrl'] = f"https://{self.bussinessRequestHost}"
        reqInfo['reqHeaders'] = self.commonHeaders
        reqInfo['reqBody'] = reqBody
        reqInfo['reqMethod'] = reqMethod
        return reqInfo
        
    # 新营业概览-收款
    def eAccountingItemsReportReq(self, storeId, startTime:int, endTime:int, platform=None):
        apiInfo = self.apisInfo['eAccountingItemsReport']
        reqMethod = apiInfo['method']
        serviceName = apiInfo['serviceName']
        reqBody = self.bussinessRequestClass(apiInfo=apiInfo, header={}, body={})
        apiUrl = self.template(self.url, {"serviceName": serviceName})
        reqBody['httpData']['uri'] = apiUrl
        reqBody['httpData']['header'] = self.httpDataHeaders
        reqBody['httpData']['body'] = {
            "startTime": startTime,
			"endTime": endTime,
			"platform": platform,
			"storeId": storeId
        }
        reqInfo = dict()
        reqInfo['reqUrl'] = f"https://{self.bussinessRequestHost}"
        reqInfo['reqHeaders'] = self.commonHeaders
        reqInfo['reqBody'] = reqBody
        reqInfo['reqMethod'] = reqMethod
        return reqInfo
    
    # 新营业概览-菜品
    def queryDishesReportReq(self, storeId, startTime:int, endTime:int, platform=None):
        apiInfo = self.apisInfo['queryDishesReport']
        reqMethod = apiInfo['method']
        serviceName = apiInfo['serviceName']
        reqBody = self.bussinessRequestClass(apiInfo=apiInfo, header={}, body={})
        apiUrl = self.template(self.url, {"serviceName": serviceName})
        reqBody['httpData']['uri'] = apiUrl
        reqBody['httpData']['header'] = self.httpDataHeaders
        reqBody['httpData']['body'] = {
            "startTime": startTime,
			"endTime": endTime,
			"platform": platform,
			"storeId": storeId
        }
        reqInfo = dict()
        reqInfo['reqUrl'] = f"https://{self.bussinessRequestHost}"
        reqInfo['reqHeaders'] = self.commonHeaders
        reqInfo['reqBody'] = reqBody
        reqInfo['reqMethod'] = reqMethod
        return reqInfo